package nc.bs.pub.action;

import java.util.List;

import nc.bs.common.projectFAUtils;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.itf.pu.m20.IPraybillMaintain;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.uap.pf.PFBusinessException;

public class N_20_SAVEBASE extends AbstractCompiler2 {
	public String getCodeRemark() {
		return "\t//####本脚本必须含有返回值,返回DLG和PNL的组件不允许有返回值####\n  nc.vo.pu.m20.entity.PraybillVO[] inObject  =(nc.vo.pu.m20.entity.PraybillVO[])getVos ();\n  Object retValue=nc.bs.framework.common.NCLocator.getInstance().lookup(nc.itf.pu.m20.IPraybillMaintain.class).insert(inObject);\n  return retValue;\n";
	}

	public Object runComClass(PfParameterVO vo) throws BusinessException {
		try {
			super.m_tmpVo = vo;
			PraybillVO[] inObject = (PraybillVO[]) ((PraybillVO[]) this.getVos());
			
			//lhw 231211 保存前校验项目 begin
			new projectFAUtils().projectDataApprove("po_praybill", inObject, "save");
			//end
			
			//humf,附件保存校验,有附件的单据不允许修改密级，采购合同
			String sql ="SELECT * FROM (select substr(filepath, INSTR(filepath, '/', 1, lengthb(regexp_replace(filepath, '[^/]', null))) - 20, 20) AS pk_bill from sm_pub_filesystem sm where ISFOLDER = 'n') pk WHERE  pk.pk_bill='"+inObject[0].getPrimaryKey()+"'  ";
            List<String> messages=(List<String>) new BaseDAO().executeQuery(sql, new ColumnListProcessor());				
			if(!messages.isEmpty()) {//有附件
				//查询是否有修改密级
				String mjsql = "SELECT vdef10 FROM po_praybill WHERE pk_praybill = '"+inObject[0].getPrimaryKey()+"'";
				List<String> dasmessages=(List<String>) new BaseDAO().executeQuery(mjsql, new ColumnListProcessor());//修改前密级
				String xghmj = inObject[0].getParentVO().getAttributeValue("vdef10")+"";//修改后密级
				if(!xghmj.equals(dasmessages.get(0))) {//密级有修改
					throw new BusinessException("单据存在附件时不可修改密级，请删除附件后重试");
				}
			}
			//end
			
			Object retValue = ((IPraybillMaintain) NCLocator.getInstance().lookup(IPraybillMaintain.class))
					.saveBase(inObject);
			return retValue;
		} catch (Exception var4) {
			if (var4 instanceof BusinessException) {
				throw (BusinessException) var4;
			} else {
				throw new PFBusinessException(var4.getMessage(), var4);
			}
		}
	}
}